package com.situ.stl.api;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.situ.stl.Service.MemberService;
import com.situ.stl.model.Member;
import com.situ.stl.util.JsonResult;
import jakarta.validation.constraints.Size;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

@RestController
@RequestMapping(value = "/member")
public class MemberApi {
    private MemberService memberService;

    @Autowired
    public void setMemberService(MemberService memberService) {
        this.memberService = memberService;
    }

    //获取对象
    @GetMapping
    public ResponseEntity<JsonResult<?>> findAll(
            @RequestParam(defaultValue = "1") Integer pageNo,
            @RequestParam(defaultValue = "10") Integer pageSize,
            Member member
    ) {
        //分页对象
        Page<Member> page = new Page<>(pageNo, pageSize);
        //查询所有会员
        page = this.memberService.findAll(page, member);

        return ResponseEntity.ok(JsonResult.success(page));
    }

    //批量查询
    @PostMapping("/batch")
    public ResponseEntity<JsonResult<?>> getByIds(@RequestBody List<Long> ids) {
        if (ids == null || ids.isEmpty()) {
            return ResponseEntity.ok(JsonResult.fail("请提供课程ID列表"));
        } else {
            return ResponseEntity.ok(JsonResult.success(memberService.getByIds(ids)));
        }
    }


    //删除]
    @DeleteMapping
    public ResponseEntity<JsonResult<?>> deleteByIds(
            @RequestBody
            @Validated
            @Size(min = 1, message = "删除的会员ID数量至少为1")
            List<Integer> ids
    ) {
        int count = this.memberService.deleteByIds(ids);
        if (count == 0) {
            return ResponseEntity.ok(JsonResult.fail("删除会员失败"));
        } else {
            return ResponseEntity.ok(JsonResult.success(count));
        }
    }

    //保存会员信息
    @PostMapping
    public ResponseEntity<JsonResult<?>> save(@RequestBody @Validated Member member) {
        boolean success = this.memberService.save(member);

        if(success) {
            return ResponseEntity.ok(JsonResult.success(member));
        } else {
            return ResponseEntity.ok(JsonResult.fail("保存会员失败"));
        }
    }
    //修改会员信息
    @PutMapping
    public ResponseEntity<JsonResult<?>> update(@RequestBody @Validated Member member) {
        boolean success = this.memberService.update(member);
        if (success) {
            return ResponseEntity.ok(JsonResult.success(member));
        } else {
            return ResponseEntity.ok(JsonResult.fail("修改会员失败"));
        }
    }
    // 充值
    @PostMapping("/recharge")
    public ResponseEntity<JsonResult<?>> recharge(
            @RequestParam Integer id,
            @RequestParam BigDecimal amount) {

        boolean success = memberService.recharge(id, amount);
        if (success) {
            return ResponseEntity.ok(JsonResult.success("充值成功"));
        } else {
            return ResponseEntity.ok(JsonResult.fail("充值失败，金额必须大于0"));
        }
    }

    // 消费
    @PostMapping("/consume")
    public ResponseEntity<JsonResult<?>> consume(
            @RequestParam Integer id,
            @RequestParam BigDecimal amount) {

        boolean success = memberService.consume(id, amount);
        if (success) {
            return ResponseEntity.ok(JsonResult.success("消费成功"));
        } else {
            return ResponseEntity.ok(JsonResult.fail("消费失败，金额必须大于0或余额不足"));
        }
    }
}
